package com.example.membersteward.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.membersteward.common.CommonResult;
import com.example.membersteward.entity.Role;
import com.example.membersteward.entity.RoleMenu;
import com.example.membersteward.entity.User;
import com.example.membersteward.exception.ServiceException;
import com.example.membersteward.mapper.RoleMenuMapper;
import com.example.membersteward.service.RoleService;
import com.example.membersteward.mapper.RoleMapper;
import com.example.membersteward.util.BaseUtils;
import com.example.membersteward.util.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
* @author 26690
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2025-04-08 22:52:37
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
    @Resource
    RoleMapper roleMapper;

    @Resource
    RoleMenuMapper roleMenuMapper;
    @Override
    public CommonResult getRoleByPage(int pageNum, int pageSize, Role role) {
        try {
            // 创建分页对象
            Page<Role> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            if (BaseUtils.isNotNull(role)) {
                if (BaseUtils.isNotEmpty(role.getDescription())) {
                    queryWrapper.like("description", role.getDescription());
                }
            }
            queryWrapper.orderByDesc("show_order");
            // 执行分页查询
            IPage<Role> userPage = roleMapper.selectPage(page, queryWrapper);
            return CommonResult.success(userPage);
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }

    @Override
    @Transactional
    public CommonResult deleteRoles(String roleIds) {
        try {
            if (BaseUtils.isEmpty(roleIds)) {
                return CommonResult.error("请检查上送内容");
            }
            String[] Ids = roleIds.split(",");
            if (BaseUtils.isEmpty(Ids)) {
                return CommonResult.error("请检查上送内容");
            }
            List<String> roleIdList = new ArrayList<>();
            for (String s : Ids) {
                if ("1".equals(s) || "2".equals(s) || "3".equals(s)) {
                    return CommonResult.error("不允许删除默认配置！");
                }
                roleIdList.add(s);
            }
            int ret = roleMapper.deleteBatchIds(roleIdList);
            if (ret <= 0) {
                return CommonResult.error("没有符合的角色，删除角色失败");
            }
            QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("role_id", Ids);
            roleMenuMapper.delete(queryWrapper);
            return CommonResult.success("删除角色成功！");
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }

    @Override
    public CommonResult insertRole(Role role) {
        try {
            role.setCreateTime(new Date());
            int ret = roleMapper.insert(role);
            if (ret <= 0) {
                return CommonResult.error("新增角色异常");
            }
            return CommonResult.success("新增角色成功！");
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }

    @Override
    public CommonResult updateRole(Role role) {
        try {
            int ret = roleMapper.updateById(role);
            if (ret <= 0) {
                return CommonResult.error("更新角色异常");
            }
            return CommonResult.success("更新角色信息成功！");
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }

    @Override
    public CommonResult getRoleIds() {
        try {
            List<Role> res = roleMapper.selectList(null);
            if (BaseUtils.isEmpty(res)) {
                return CommonResult.error("查询无记录");
            }
            List<Map<String, String>> list = new ArrayList<>();
            for (Role role : res) {
                Map<String, String> map = new HashMap<>();
                map.put("label",role.getDescription());
                map.put("value", String.valueOf(role.getRoleId()));
                list.add(map);
            }
            return CommonResult.success(list);
        } catch (Exception e) {
            logger.error("执行异常", e);
            throw new ServiceException("执行异常");
        }
    }
}




